<?php
/**
 * Created by PhpStorm.
 * User: zhang
 * Date: 2018/9/7
 * Time: 23:26
 */

namespace app\student\model;

use app\exam\model\ExamScheduleManager;
use app\teacher\model\TeacherExamManager;

class StudentExamManager extends ExamScheduleManager {
    const DB_NAME = "exam_registration";

    const EXAMING = 0;      //表示已经报名中
    const OUT_EXAM = 1;

    const ERROR_EXAMING = "已经有报名，请勿重复报名";
    const ERROR_EXAMFULL = "考场报名已满";
    const ERROR_TIME = "下一次报考需要间隔7天";
    const ERROR_PASS="考试及格不允许继续报名考试";
    const BEYOND_DAY = 7;

    const OVER_EXAM = "考试已结束";

    public function __construct()
    {
        parent::__construct();
    }

    public function getExamRegisterByStudentId($student_id){
        $sql = "select A.student_id,C.name,A.exam_id,B.date,B.place,A.status,A.score from exam_registration 
AS A INNER join exam_schedule AS B on A.exam_id=B.id INNER join student AS C on A.student_id=C.id where A.student_id='$student_id'";

        $result = $this->getExamRegisterByCustom(null,$sql);
        return $result;
    }

    public function createExamRegisterByStudentId($student_id,$exam_id){
        $score = $this->getExamScoreByStudentId($student_id);

        if($score >= 60){
            echoJson(0, self::ERROR_PASS);
        }
        //        //检查是否报名
        if($this->checkExamRegisterExistByStudentId($student_id)){
            echoJson(0, self::ERROR_EXAMING);
        }

        if(!$this->checkExamBeyondDaysByStudentId($student_id,$exam_id)){
            echoJson(0, self::ERROR_TIME);
        }

        $result = $this->createExamRegisterByCustom(array("student_id"=>$student_id,"exam_id"=>$exam_id,"status"=>self::EXAMING));

        $sql = "update ".parent::DB_NAME." set current_mount=current_mount+1 where id=".$exam_id." and current_mount<total_mount";
        if(!$this->updateSQL($sql)){
            echoJson(0, self::ERROR_EXAMFULL);
        }
        return $result;
    }

    private function getExamScoreByStudentId($student_id){
        $sql = "select max(score) as score from ".self::DB_NAME." where student_id=".$student_id;
        $result = $this->selectSQLbyCustom(null,null,$sql);
        return $result[score];
    }

    private function checkExamBeyondDaysByStudentId($student_id,$exam_id){

        $sql = "select * from exam_schedule where id='$exam_id'";
        $result = $this->selectSQLbyCustom(null,null,$sql);

        if($result["status"] == 2){
            echoJson(0, self::OVER_EXAM);
        }else if($result["status"] == 1){
            echoJson(0, self::ERROR_EXAMFULL);
        }

        $sql = "select A.student_id,C.name,A.exam_id,B.date,B.place,A.status from exam_registration 
AS A INNER join exam_schedule AS B on A.exam_id=B.id INNER join student AS C on A.student_id=C.id where A.student_id='$student_id' Order By date desc";
        $result = $this->selectSQLbyCustom(null,null,$sql);

//        var_dump($result);
//        die();
//        var_dump($sql);
        $day = gettime()-date('YmdHis',strtotime($result["date"]));
        $day = ($day/1000000);

//        var_dump(gettime());
//        var_dump(date('YmdHis',strtotime($result["date"])));
        if(count($result)==0){
            return true;
        }else
        if($day >= self::BEYOND_DAY){
            //$this->updateSQLbyCustom(self::DB_NAME,array("student_id"=>$result["student_id"],"exam_id"=>$result["exam_id"],"status"=>"0"),array("status"=>"1"));
            return true;
        }

        return false;
    }

    //检查是否有报名存在
    private function checkExamRegisterExistByStudentId($student_id){
        $sql = "select A.student_id,C.name,A.exam_id,B.date,B.place,A.status from exam_registration 
AS A INNER join exam_schedule AS B on A.exam_id=B.id INNER join student AS C on A.student_id=C.id where A.status=".self::EXAMING." and A.student_id='$student_id'";
        $result = $this->selectSQLbyCustom(null,null,$sql);
        //array("student_id"=>$student_id,"status"=>self::EXAMING)
        $day = gettime()-date('YmdHis',strtotime($result["date"]));
        $day = ($day/1000000);

        if($day<0)
            return true;

        return false;
    }

    private function getExamRegisterByCustom($customKey=[],$sql=null){
        return $this->selectSQLAllbyCustom(self::DB_NAME,$customKey,$sql);
    }

    private function createExamRegisterByCustom($saveSequence=[]){
        return $this->insertSQLbyCustom(self::DB_NAME,$saveSequence);
    }

    private function updateExamRegisterByCustom($saveCondition,$saveSequence=[]){
        return $this->updateSQLbyCustom(self::DB_NAME, $saveCondition,$saveSequence);
    }
}